﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// 隐式转换和显式转换示例:
namespace CH02_01
{
    class CH02_01
    {
        static void Main(string[] args)
        {
            // 1.隐式转换:int类型转换为double类型;
            int iNumber1 = 100;
            double dNumber1 = iNumber1;

            Console.WriteLine("隐式转换示例如下:");
            Console.WriteLine("转换前的值为:" + iNumber1);
            Console.WriteLine("转换后的值为:" + dNumber1 + "\n");

            // 2.显式转换
            // double类型转int类型:
            double dNumber2 = 3.14;
            int iNumber2 = (int)dNumber2;
            
            Console.WriteLine("显式转换double类型转int类型示例:");
            Console.WriteLine("(double to int)转换前的值为:" + dNumber2);
            Console.WriteLine("(double to int)转换后的值为:" + iNumber2 + "\n");

            // string类型转换为其他类型:
            string sData1 = "584";
            string sData2 = "5.84";
            int strToInt = int.Parse(sData1);
            double strToDouble = double.Parse(sData2);

            Console.WriteLine("显式转换string类型转其他类型示例:");
            Console.WriteLine("(string to int)转换前的值为:" + sData1);
            Console.WriteLine("(string to int)转换后的值为:" + strToInt);
            Console.WriteLine("(string to double)转换后的值为:" + sData2);
            Console.WriteLine("(string to double)转换后的值为:" + strToDouble + "\n");

            // 其他类型转换为string类型:
            int iNumber3 = 584;
            double dNumber3 = 5.84;
            string intToStr = iNumber3.ToString();
            string doubleToStr = dNumber3.ToString();
            Console.WriteLine("其他类型转换为string类型示例:");
            Console.WriteLine("(int to string)转换前的值为:" + iNumber3);
            Console.WriteLine("(int to string)转换后的值为:" + intToStr);
            Console.WriteLine("(double to string)转换前的值为:" + dNumber3);
            Console.WriteLine("(double to string)转换后的值为:" + doubleToStr);
        }
    }
}
